package com.luyou.unit4;

/**
 *
 * double等效性
 * @author weiqi
 * @version 1.0
 * Create by 2024/8/1 8:48
 */
public class DoubleEquivalence {

    static void show(String desc, Double n1, Double n2){
        System.out.println(desc + ":");
        System.out.printf("%e==%e %b %b%n", n1, n2, n1==n2, n1.equals(n2));
    }

    @SuppressWarnings("deprecation")
    public static void test(double x1, double x2){
        System.out.printf("%e==%e %b%n", x1, x2, x1 == x2);
        Double d1 = x1;
        Double d2 = x2;
        show("Automatic", d1, d2);
        Double v1 = Double.valueOf(x1);
        Double v2 = Double.valueOf(x2);
        show("Double.valueOf()", v1, v2);
    }

    public static void main(String[] args) {
        test(0, Double.MIN_VALUE);
        System.out.println("------------------------");
        //舍入误差，这种误差之所以发生，是因
        //为机器不能存储足够的信息来表示一个大数值微笑变化
        /**
         * 1.797693e+308==1.797693e+308 true
         * Automatic:
         * 1.797693e+308==1.797693e+308 false true
         */
        test(Double.MAX_VALUE, Double.MAX_VALUE - Double.MIN_VALUE * 1_000_000);
    }

}
